﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataLayer;

namespace OrderDocument
{
    public partial class frmYeniGiris : Form
    {
        public bool EskiKayit { get; set; }
        public decimal Miktar { get; set; }

        public frmYeniGiris()
        {
            InitializeComponent();
        }

        private void txtBarkod_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtAdi.Focus();
            }
        }

        private void txtAdi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtFiyat.Focus();
            }
        }

        private void txtFiyat_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtKdv.Focus();
            }
        }

        public void barkodBul(string barkod)
        {
            DataTable dt = Factory.GetFactory.ExecuteDataTable(string.Format("SELECT barcode, code, name, currency, price, vat, supplier, new, unit, discount1, discount2, discount3 FROM PRODUCTS WHERE     (barcode = '{0}')", barkod));
            if (dt.Rows.Count > 0)
            {
                txtKodu.Text = dt.Rows[0]["code"].ToString();
                txtBarkod.Text = dt.Rows[0]["barcode"].ToString();
                txtAdi.Text = dt.Rows[0]["name"].ToString();
                txtFiyat.Text = Convert.ToDecimal(dt.Rows[0]["price"]).ToString();
                txtKdv.Text = dt.Rows[0]["vat"].ToString();
                txtbirim.Text = dt.Rows[0]["unit"].ToString();
                switch (dt.Rows[0]["currency"].ToString())
                {
                    case "0":
                        cmbDoviz.Text = "TL";
                        break;
                    case "1":
                        cmbDoviz.Text = "USD";
                        break;
                    case "2":
                        cmbDoviz.Text = "EURO";
                        break;
                    default:
                        cmbDoviz.SelectedIndex = 0;
                        break;
                }
                txtIskonto.Text = dt.Rows[0]["discount1"].ToString();
                txtiskonto2.Text = dt.Rows[0]["discount2"].ToString();
                txtiskonto3.Text = dt.Rows[0]["discount3"].ToString();
            }
        }

        private string CheckBarkod(string barkod)
        {
            IDbDataParameter prm = Factory.GetFactory.GetParameter("@barkod", DbType.String);
            prm.Value = txtBarkod.Text;
            object deger = Factory.GetFactory.ExecuteScaler(CommandType.Text, "SELECT     supplier FROM         PRODUCTS  WHERE     (barcode = @barkod)", prm);
            if (deger != null)
                return deger.ToString();
            else
                return "";
        }


        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtMiktar.Text))
            {
                this.Miktar = decimal.Parse(txtMiktar.Text);
            }
            else
            {
                this.Miktar = 1;
            }
            Kaydet();
        }

        private void Kaydet()
        {
            if (!string.IsNullOrEmpty(txtKodu.Text) && !string.IsNullOrEmpty(txtBarkod.Text) && !string.IsNullOrEmpty(txtAdi.Text) && !string.IsNullOrEmpty(txtFiyat.Text) && !string.IsNullOrEmpty(txtKdv.Text))
            {
                //string suplier = CheckBarkod(txtBarkod.Text);
                //if (string.IsNullOrEmpty(suplier))
                //{
                IDbDataParameter stokkod = Factory.GetFactory.GetParameter("@stokkodu", DbType.String);
                stokkod.Value = txtKodu.Text;
                IDbDataParameter barkod = Factory.GetFactory.GetParameter("@barkod", DbType.String);
                barkod.Value = txtBarkod.Text;
                IDbDataParameter birim = Factory.GetFactory.GetParameter("@birim", 25, DbType.String);
                birim.Value = txtbirim.Text;
                IDbDataParameter adi = Factory.GetFactory.GetParameter("@adi", DbType.String);
                adi.Value = txtAdi.Text;
                IDbDataParameter kdv = Factory.GetFactory.GetParameter("@kdv", DbType.Int32);
                kdv.Value = int.Parse(txtKdv.Text);
                IDbDataParameter fiyat = Factory.GetFactory.GetParameter("@fiyat", DbType.Decimal);
                fiyat.Value = Convert.ToDecimal(txtFiyat.Text.Replace('.', ','));
                IDbDataParameter satici = Factory.GetFactory.GetParameter("@satici", DbType.String);
                satici.Value = System.Configuration.ConfigurationSettings.AppSettings["CariKodu"];
                IDbDataParameter doviz = Factory.GetFactory.GetParameter("@doviz", DbType.Int32);
                doviz.Value = cmbDoviz.SelectedIndex;
                IDbDataParameter iskonto = Factory.GetFactory.GetParameter("@iskonto", DbType.Decimal);
                if (!string.IsNullOrEmpty(txtIskonto.Text)) iskonto.Value = Convert.ToDecimal(txtIskonto.Text.Replace('.', ',')); else iskonto.Value = 0m;

                IDbDataParameter iskonto2 = Factory.GetFactory.GetParameter("@iskonto2", DbType.Decimal);
                if (!string.IsNullOrEmpty(txtiskonto2.Text)) iskonto2.Value = Convert.ToDecimal(txtiskonto2.Text.Replace('.', ',')); else iskonto2.Value = 0m;

                IDbDataParameter iskonto3 = Factory.GetFactory.GetParameter("@iskonto3", DbType.Decimal);
                if (!string.IsNullOrEmpty(txtiskonto3.Text)) iskonto3.Value = Convert.ToDecimal(txtiskonto3.Text.Replace('.', ',')); else iskonto3.Value = 0m;


                IDbDataParameter marka = Factory.GetFactory.GetParameter("@marka", DbType.String);
                marka.Value = txtMarka.Text;

                IDbDataParameter ith = Factory.GetFactory.GetParameter("@ithalat", DbType.String);
                ith.Value = txtIthalat.Text;

                IDbDataParameter ihr = Factory.GetFactory.GetParameter("@ihracat", DbType.String);
                ihr.Value = txtIhracat.Text;

                Factory.GetFactory.ExecuteNonQuery(CommandType.Text, "if exists(select barcode from PRODUCTS where barcode=@barkod) update PRODUCTS set name=@adi,code=@stokkodu, currency=@doviz, price=@fiyat, vat=@kdv, new=1,unit=@birim,discount1=@iskonto,discount2=@iskonto2,discount3=@iskonto3,brand_code=@marka,export_code=@ithalat,import_code=@ihracat where barcode=@barkod  else INSERT INTO PRODUCTS (barcode, name, currency, price, vat, supplier, new,code,unit,discount1,discount2,discount3,brand_code,export_code,import_code) VALUES     (@barkod, @adi, @doviz, @fiyat, @kdv, @satici,1,@stokkodu,@birim,@iskonto,@iskonto2,@iskonto3,@marka,@ithalat,@ihracat)", barkod, adi, doviz, fiyat, kdv, satici, birim, iskonto, iskonto2, iskonto3, marka, ith, ihr, stokkod);
                DialogResult = DialogResult.OK;
                this.Close();
                //}
                //else
                //    MessageBox.Show("Bu Barkodlu Ürün Kaydı Bulunmaktadır Satıcı Kodu :" + suplier);
            }
            else
            {
                MessageBox.Show("Kodu,Adı,Barkodu,Fiyatı,Kdv alanlarının girilmesi zorunludur !");
            }
        }

        private void YazdikcaAra()
        {
            string[] Skelimeler = txtAdi.Text.Split(' ');
            string[] Bkelimeler = txtBarkod.Text.Split(' ');

            List<string> where = new List<string>();
            foreach (string ad in Skelimeler)
                where.Add(" (name LIKE '%" + ad + "%') ");
            foreach (string brk in Bkelimeler)
                where.Add(" (barcode LIKE '%" + brk + "%') ");

            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT     barcode AS Barkod, name AS [Ürün Adı]  FROM PRODUCTS WHERE supplier = '" + System.Configuration.ConfigurationSettings.AppSettings["CariKodu"] + "' AND ");
            foreach (string q in where)
                sb.Append(q + " and ");

            sb.Remove(sb.Length - 4, 4);


            DataTable dt = Factory.GetFactory.ExecuteDataTable(CommandType.Text, sb.ToString());
            if (dt.Rows.Count > 0)
            {
                gridView1.Columns.Clear();
                gridControl1.DataSource = dt;
                this.Height = 437;
                panelControl2.Visible = true;
            }
            else
            {
                gridView1.Columns.Clear();
                this.Height = 98;
                panelControl2.Visible = false;
            }
        }


        private void txtAdi_TextChanged(object sender, EventArgs e)
        {
            if (!EskiKayit)
            {
                if (!string.IsNullOrEmpty(txtAdi.Text))
                {
                    YazdikcaAra();
                }
            }
        }

        private void txtBarkod_TextChanged(object sender, EventArgs e)
        {
            if (!EskiKayit)
            {
                if (!string.IsNullOrEmpty(txtBarkod.Text))
                {
                    YazdikcaAra();
                }
            }
        }

        private void comboBoxEdit1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                cmbDoviz.Focus();
        }

        private void txtKdv_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtbirim.Focus();
        }

        private void cmbDoviz_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DialogResult rs = MessageBox.Show("Kaydedilsin mi?", "", MessageBoxButtons.YesNo);
                if (rs == DialogResult.Yes)
                    Kaydet();
            }
        }

        private void frmYeniGiris_Load(object sender, EventArgs e)
        {

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void txtKodu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtBarkod.Focus();
            }
        }
    }
}
